package com.huanglaoshi.java.thread;



class 售票窗口02 implements Runnable{
	
	private int ticket;
	
	public 售票窗口02(int ticket) {
		this.ticket = ticket;
	}



	public void run(){
		
		while(true){
			synchronized (this) {
				卖票();
			}
		}
		
	}
	
	
	public synchronized void 卖票(){
		if (ticket >= 1) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"售出第" + ticket-- + "张票...");
		}
	}
	
}


public class 五个窗口卖100张票_同步方法 {
	
	public static void main(String[] args) {
		售票窗口02 w = new 售票窗口02(100);
		
		Thread t01 = new Thread(w);
		Thread t02 = new Thread(w);
		Thread t03 = new Thread(w);
		Thread t04 = new Thread(w);
		Thread t05 = new Thread(w);
		
		t01.setName("01窗口");
		t02.setName("02窗口");
		t03.setName("03窗口");
		t04.setName("04窗口");
		t05.setName("05窗口");
		
		t01.start();
		t02.start();
		t03.start();
		t04.start();
		t05.start();
	}

}
